//
//  NSString+Tools.m
//  OhMyBaby
//
//  Created by JPlay on 14-5-22.
//  Copyright (c) 2014年 JPlay. All rights reserved.
//

#import "NSString+Tools.h"
#import <CommonCrypto/CommonDigest.h>
#import <UIKit/UIKit.h>

@implementation NSString (Tools)


+(NSString *)MD5WithString:(NSString *)string{
    const char *cStr = [string UTF8String];
    unsigned char result[16];
    CC_MD5( cStr, (int)strlen(cStr), result );
    return [NSString stringWithFormat:
            @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]
            ];
    
}

// 隐藏index开始到剩余4位的地方
+(NSString *)CardSecurityString:(NSString *)string andFirstShowIndex:(int)index
{
    int Stringlenth = (int)string.length;
    if (Stringlenth < 5 || Stringlenth < index+4)
    {
        return string;
    }
    NSString *StringFirst = [string substringToIndex:index];
    NSString *StringLaster = [string substringFromIndex:Stringlenth-4];
    int count = Stringlenth-index-4;
    for (int i=0; i< count; i++)
    {
        StringFirst = [StringFirst stringByAppendingString:@"*"];
    }
    StringFirst = [StringFirst stringByAppendingString:StringLaster];
    
    return StringFirst;
}

+(NSString *)showSecurityString:(NSString *)string andSurplusCount:(int)count
{
    int Stringlenth = (int)string.length;
    if (Stringlenth <= count)
    {
        return string;
    }
    NSString *StringFirst = [string substringToIndex:Stringlenth-count];
    for (int i = 0; i<count; i++)
    {
        StringFirst = [StringFirst stringByAppendingString:@"*"];
    }
    return StringFirst;
}

/*+ (BOOL)LoginJudgeAndShowView
{
    UIViewController *result;
    
    // Try to find the root view controller programmically
    
    // Find the top window (that is not an alert view or other window)
    
    UIWindow *topWindow = [[UIApplication sharedApplication] keyWindow];
    if (topWindow.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(topWindow in windows)
        {
            if (topWindow.windowLevel == UIWindowLevelNormal)
                break;
        }
    }
    UIView *rootView = [[topWindow subviews] objectAtIndex:([[topWindow subviews] count]-1)];
    
    id nextResponder = [rootView nextResponder];
    if ([nextResponder isKindOfClass:[UIViewController class]])
    {
        result = nextResponder;
    }
    else {
        result = topWindow.rootViewController;
    };
    
    NSUserDefaults *userDefaluts = [NSUserDefaults standardUserDefaults];
    NSString *StringUserID = [userDefaluts objectForKey:KEY_USER_ID];
    if (StringUserID == nil)
    {
        [API sharedInstance].g_IfComeFromBackgroud = NO;
        [API sharedInstance].g_IfProductBuyLogin = 4;
        UIStoryboard *stryBoard=[UIStoryboard storyboardWithName:@"Login" bundle:nil];
        //[stryBoard instantiateInitialViewController]
        [((UINavigationController *)result.view.window.rootViewController).topViewController presentViewController:[stryBoard instantiateInitialViewController] animated:YES completion:^{}];

        return NO;
    }
    return YES;
}*/

/*+ (BOOL)UnlockPasswordOpen
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSNumber *result = [userDefaults objectForKey:KEY_IF_UNLOCK_OPEN];
    if (result == nil || [result intValue] == 0)
    {
        return NO;
    }
    else if([result intValue] == 1)
    {
        return YES;
    }
    return NO;
}*/

+ (BOOL)JudgePasswordCorrect:(NSString *)Password
{
    // (?=^.{8,}$)(?=.*\d)(?=.*\W+)(?=.*[A-Z])(?=.*[a-z])(?!.*\n).*$
    // ^(?![0-9]+$)[a-zA-Z0-9_~!@#$%\^&*]{6,16}$
    // ^(?![0-9]+$)[a-zA-Z0-9_~!@#$%\\^&*]{6,16}$
    NSString *regex = @"^(?![0-9]+$)[a-zA-Z0-9_~!@#$%\\^&*]{6,16}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    if ([pred evaluateWithObject:Password])
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

+ (BOOL)JudgePayPasswordCorrect:(NSString *)Password
{
    NSString *regex = @"^\\d{6}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    if ([pred evaluateWithObject:Password])
    {
        return YES;
    }
    else
    {
        return NO;
    }
    return YES;
}

+ (BOOL)JudgeWithdawalMoney:(NSString *)Money
{
    //
    NSString *regex = @"^\\d+(\\.\\d{0,2})?$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    if ([pred evaluateWithObject:Money])
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

+ (BOOL)checkPhoneNumInput:(NSString *)Phone;
{
    if (Phone.length < 5)
    {
        return NO;
    }
    
    NSString * StringPhone = @"^1([0-9])\\d{9}";
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", StringPhone];
    BOOL res1 = [regextestmobile evaluateWithObject:Phone];
    
    if (res1)
    {
        return YES;
    }
    else
    {
        return NO;
    }
    
}

/*// 拼接图片下载地址
+(NSURL *)ImagePathForServer:(NSString *)imagePath
{
    NSString *stringPath = [NSString stringWithFormat:@"%@%@",IMAGE_SERVER,imagePath];
    return [NSURL URLWithString:stringPath];
}

+ (NSString *)getUserID
{
    NSUserDefaults *userDefaluts = [NSUserDefaults standardUserDefaults];
    NSString *StringUserID = [userDefaluts objectForKey:KEY_USER_ID];
    return StringUserID;
}*/

+(NSString *)countNumAndChangeformat:(NSString *)num
{
    int count = 0;
    long long int a = num.longLongValue;
    while (a != 0)
    {
        count++;
        a /= 10;
    }
    NSMutableString *string = [NSMutableString stringWithString:num];
    NSMutableString *newstring = [NSMutableString string];
    while (count > 3)
    {
        count -= 3;
        NSRange rang = NSMakeRange(string.length - 3, 3);
        NSString *str = [string substringWithRange:rang];
        [newstring insertString:str atIndex:0];
        [newstring insertString:@"," atIndex:0];
        [string deleteCharactersInRange:rang];
    }
    [newstring insertString:string atIndex:0];
    return newstring;
}

@end
